using System;
using System.Drawing;
using System.ComponentModel;

namespace WebChart
{
	public class StackedColumnChart : Chart
	{
		// Fields
		private int maxColumnWidth;

		// Methods
		public StackedColumnChart()
		{
		}

		public StackedColumnChart(ChartPointCollection data) : base(data)
		{
		}

		public StackedColumnChart(ChartPointCollection data, Color color) : base(data, color)
		{
		}

		public override void GetMinMaxMeanValue(out float min, out float max, out float mean)
		{
			ChartEngine engine = base.Engine;
			min = 0f;
			max = 0f;
			mean = 0f;
			int stackedCount = this.GetStackedCount(engine.Charts);
			if ((stackedCount > 0) && (engine.MaxPoints > 0))
			{
				Chart[] chartArray = new Chart[stackedCount];
				int num2 = 0;
				foreach (Chart chart in engine.Charts)
				{
					if (chart is StackedColumnChart)
					{
						chartArray[num2++] = chart;
					}
				}
				float num3 = 0f;
				min = 0f;
				max = float.MinValue;
				for (int i = 0; i < engine.MaxPoints; i++)
				{
					float num6 = 0f;
					float num7 = 0f;
					for (int j = 0; j < stackedCount; j++)
					{
						ChartPointCollection data = chartArray[j].Data;
						if (data.Count > i)
						{
							float yvalue = data[i].yvalue;
							if (yvalue >= 0f)
							{
								num6 += yvalue;
							}
							else
							{
								num7 += yvalue;
							}
						}
					}
					if (num7 < min)
					{
						min = num7;
					}
					if (num6 > max)
					{
						max = num6;
					}
					num3 += num6 + num7;
				}
				mean = num3 / ((float) engine.MaxPoints);
				if (base.dataLabels.visible)
				{
					float num9 = (max - min) / 10f;
					max += num9;
				}
			}
		}

		private int GetStackedCount(ChartCollection charts)
		{
			int num = 0;
			bool flag = true;
			foreach (Chart chart in charts)
			{
				if (!(chart is StackedColumnChart))
				{
					continue;
				}
				if (flag)
				{
					if (chart != this)
					{
						return 0;
					}
					flag = false;
				}
				num++;
			}
			return num;
		}

		public override void Render(Graphics graphics, int width, int height)
		{
			ChartEngine engine = base.Engine;
			int stackedCount = this.GetStackedCount(engine.Charts);
			if (stackedCount != 0)
			{
				StackedColumnChart[] chartArray = new StackedColumnChart[stackedCount];
				Pen[] penArray = new Pen[stackedCount];
				Brush[] brushArray = new Brush[stackedCount];
				int index = 0;
				bool flag = (base.shadow != null) && base.Shadow.Visible;
				Brush brush = null;
				if (flag)
				{
					brush = new SolidBrush(base.Shadow.Color);
				}
				foreach (Chart chart in engine.Charts)
				{
					chartArray[index] = chart as StackedColumnChart;
					if (chartArray[index] != null)
					{
						penArray[index] = chart.Line.GetPen(graphics);
						brushArray[index] = chart.Fill.GetBrush(graphics);
						index++;
						if (index >= stackedCount)
						{
							break;
						}
					}
				}
				float scaleX = engine.scaleX;
				float scaleY = engine.scaleY;
				int num5 = 0;
				int maxColumnWidth = (int) scaleX;
				if ((this.maxColumnWidth > 0) && (this.maxColumnWidth < scaleX))
				{
					num5 = (((int) scaleX) - this.maxColumnWidth) / 2;
					maxColumnWidth = this.maxColumnWidth;
				}
				for (int i = 0; i < engine.MaxPoints; i++)
				{
					float num9 = 0f;
					float num10 = 0f;
					for (int j = 0; j < stackedCount; j++)
					{
						ChartPointCollection data = chartArray[j].Data;
						if (data.Count > i)
						{
							float y;
							float num15;
							float yvalue = data[i].yvalue;
							float x = (i * scaleX) + num5;
							float num13 = maxColumnWidth - 1f;
							if (yvalue > 0f)
							{
								num9 += yvalue;
								y = num9 * scaleY;
								num15 = -yvalue * scaleY;
								if (flag)
								{
									graphics.FillRectangle(brush, x + base.shadow.OffsetX, y + base.shadow.OffsetY, num13, num15);
								}
								graphics.FillRectangle(brushArray[j], x, y, num13, num15);
								graphics.DrawRectangle(penArray[j], x, y, num13, num15);
							}
							else if (yvalue < 0f)
							{
								y = num10 * scaleY;
								num15 = yvalue * scaleY;
								num10 += yvalue;
								if (flag)
								{
									graphics.FillRectangle(brush, x + base.shadow.OffsetX, y + base.shadow.OffsetY, num13, num15);
								}
								graphics.FillRectangle(brushArray[j], x, y, num13, num15);
								graphics.DrawRectangle(penArray[j], x, y, num13, num15);
							}
						}
					}
					if (base.dataLabels.visible)
					{
						this.RenderDataLabels(graphics);
					}
				}
			}
		}

		protected override void RenderDataLabels(Graphics graphics)
		{
			if (base.dataLabels.font == null)
			{
				base.dataLabels.font = ChartText.DefaultFont;
			}
			ChartEngine engine = base.Engine;
			int maxWidth = base.DataLabelMaxWidth;
			StringFormat format = new StringFormat();
			format.Alignment = StringAlignment.Center;
			format.LineAlignment = StringAlignment.Near;
			int stackedCount = this.GetStackedCount(engine.Charts);
			if (stackedCount != 0)
			{
				StackedColumnChart[] chartArray = new StackedColumnChart[stackedCount];
				Pen[] penArray = new Pen[stackedCount];
				Brush[] brushArray = new Brush[stackedCount];
				Brush[] brushArray2 = new Brush[stackedCount];
				int index = 0;
				foreach (Chart chart in engine.Charts)
				{
					chartArray[index] = chart as StackedColumnChart;
					if (chartArray[index] != null)
					{
						penArray[index] = chart.dataLabels.border.GetPen(graphics);
						brushArray[index] = chart.dataLabels.background.GetBrush(graphics);
						brushArray2[index] = new SolidBrush(chart.dataLabels.foreColor);
						index++;
						if (index >= stackedCount)
						{
							break;
						}
					}
				}
				float scaleX = engine.scaleX;
				float scaleY = engine.scaleY;
				for (int i = 0; i < engine.MaxPoints; i++)
				{
					float num6 = 0f;
					float num7 = 0f;
					for (int j = 0; j < stackedCount; j++)
					{
						ChartPointCollection data = chartArray[j].Data;
						if (data.Count > i)
						{
							RectangleF rectangle;
							ChartPoint point = data[i];
							float yvalue = point.yvalue;
							string dataLabelText = base.GetDataLabelText(point);
							if (yvalue > 0f)
							{
								num6 += yvalue;
								rectangle = base.GetDataLabelRectangle(graphics, num6, dataLabelText, maxWidth, i);
								base.DrawDataLabel(graphics, dataLabelText, rectangle, penArray[j], brushArray[j], format, brushArray2[j]);
							}
							else if (yvalue < 0f)
							{
								rectangle = base.GetDataLabelRectangle(graphics, num7, dataLabelText, maxWidth, i);
								num7 += yvalue;
								base.DrawDataLabel(graphics, dataLabelText, rectangle, penArray[j], brushArray[j], format, brushArray2[j]);
							}
						}
					}
				}
			}
		}

		// Properties
		[DefaultValue(0)]
		public int MaxColumnWidth
		{
			get
			{
				return this.maxColumnWidth;
			}
			set
			{
				this.maxColumnWidth = value;
			}
		}
	}
}
